package com.manager.user.module;

import com.manager.core.module.manager.service.MainService;
import com.manager.core.web.json.AjaxJson;
import com.manager.user.web.controller.AdminController;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


/**
 * 控制器
 *
 * @author H
 * @version 1.0
 */
@Controller
public class MainController extends AdminController<String> {
    @Autowired
    private MainService _service;

    /**
     * 欢迎页
     *
     * @return
     */
    @RequestMapping("/welcome")
    public ModelAndView welcome() {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return getModelAndView("login");
        }
        return getModelAndView("center", _service.findAccountView(getParams()));
    }


    /**
     * 欢迎页
     *
     * @return
     */
    @RequestMapping("/center")
    public ModelAndView center() {
        if (getUserSession() != null) {
            request.setAttribute("name", getUserSession().getUserName());
        }

        return getModelAndView("center", _service.findAccountView(getParams()));
    }

    /**
     * 首页
     *
     * @return
     */
    @RequestMapping("/login")
    public ModelAndView login() {
        return getModelAndView("login");
    }

    /**
     * 首页
     *
     * @return
     */
    @RequestMapping("/home")
    public ModelAndView home() {
        return getModelAndView("home");
    }

    /**
     * 首页
     *
     * @return
     */
    @RequestMapping("/logout")
    public ModelAndView logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        getSession().invalidate();
        return getModelAndView("login");
    }

    /**
     * 详情页面
     *
     * @return
     */
    @RequestMapping("/user/info")
    public ModelAndView info() {
        return getModelAndView("platform/user/user-info", _service.findAccountView(getParams()));
    }

    @RequestMapping("/user/pwd")
    public ModelAndView pwd() {
        return getModelAndView("platform/user/update-password");
    }

    /**
     * 修改密码
     *
     * @return
     */
    @PostMapping(value = "/user/pwd/save")
    public @ResponseBody AjaxJson personalPwdModify() {
        return _service.updatePwd(getParams());
    }

    @Override
    public String getModel() {
        return null;
    }
}